package com.thejaswi.commons;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.io.File;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 *
 * @author BhatG
 */
public class Cryptographer {

   public static final String AES = "AES";
   public String algorithm = Cryptographer.AES;
   public int keySize = 128;

   public String encrypt (String inString, String keyString)
         throws GeneralSecurityException, NoSuchAlgorithmException {
      String encryptedString="";
      SecretKeySpec secretKeySpec = new SecretKeySpec(hexStringToByteArray(keyString), this.algorithm);
      Cipher cipher = Cipher.getInstance(this.algorithm);
      cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, cipher.getParameters());
      byte[] encrypted=cipher.doFinal(inString.getBytes());
      encryptedString = new String(byteArrayToHexString(encrypted));
      return encryptedString;
   }

   public String decrypt(String encryptedString, String keyString)
         throws GeneralSecurityException, NoSuchAlgorithmException {
      String decryptedString="";
      byte[] keyBytes = this.hexStringToByteArray(keyString);
      SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, this.algorithm);
      Cipher cipher = Cipher.getInstance(this.algorithm);
      cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
      byte[] decrypted = cipher.doFinal(hexStringToByteArray(encryptedString));
      decryptedString = new String(decrypted);
      return decryptedString;
   }

   public String readKeyFile(String keyFilePath) throws FileNotFoundException {
      String keyString="";
      File keyFile = new File(keyFilePath);
      Scanner scanner = new Scanner(keyFile).useDelimiter("\\Z");
      keyString=scanner.next();
      scanner.close();
      return keyString;
   }
   
   public byte[] generateKeyByteArray() throws GeneralSecurityException {
      KeyGenerator keyGenerator = KeyGenerator.getInstance(this.algorithm);
      keyGenerator.init(this.keySize);
      SecretKey secretKey = keyGenerator.generateKey();
      return secretKey.getEncoded();
   }
   public String generateKeyHexString() throws GeneralSecurityException {
      return byteArrayToHexString(this.generateKeyByteArray());
   }

   public void writeKeyFile(byte[] outputByteArray, String keyFilePath) throws IOException {
      this.writeKeyFile(byteArrayToHexString(outputByteArray), keyFilePath);
   }
   
   public void writeKeyFile(String outputString, String keyFilePath) throws IOException {
      FileWriter fileWriter = new FileWriter(new File(keyFilePath));
      fileWriter.write(outputString);
      fileWriter.flush();
      fileWriter.close();
   }

   public String byteArrayToHexString (byte[] b) {
      StringBuffer sb = new StringBuffer(b.length * 2);
      for (int i = 0; i < b.length; i++){
         int v = b[i] & 0xff;
         if (v < 16) {
           sb.append('0');
         }
         sb.append(Integer.toHexString(v));
      }
      return sb.toString().toUpperCase();
   }
   public byte[] hexStringToByteArray (String s) {
      byte[] b = new byte[s.length() / 2];
      for (int i = 0; i < b.length; i++){
        int index = i * 2;
        int v = Integer.parseInt(s.substring(index, index + 2), 16);
        b[i] = (byte)v;
      }
      return b;
   }
}